package com.pengpai.pangucms.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pengpai.pangucms.entity.WebConfigProperty;
import com.pengpai.pangucms.service.IWebConfigPropertyService;
import com.pengpai.pangucms.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rxg
 * @since 2023-09-11
 */
@Controller
@RequestMapping("/webConfigProperty")
public class WebConfigPropertyController {

    @Autowired
    private IWebConfigPropertyService WebConfigPropertyService;

    @GetMapping("/index")
    @PreAuthorize("hasAuthority('web:config:read')")
    public String index(){
        return "admin/web_config_list/web_config_list_index";
    }

    @GetMapping("/index2")
    @PreAuthorize("hasAuthority('web:config:read')")
    public String index2(Model model){
        WebConfigProperty webConfig = WebConfigPropertyService.getById(1);
        model.addAttribute("config",webConfig);
        return "admin/web_config_list/web_config_list_edit";
    }

    @GetMapping("/add")
    @PreAuthorize("hasAuthority('web:config:update')")
    public String add(){
        return "admin/web_config_list/web_config_list_add";
    }

    @GetMapping("edit")
    @PreAuthorize("hasAuthority('web:config:update')")
    public String edit(Model model, String id){
        WebConfigProperty webConfig = WebConfigPropertyService.getById(id);
        model.addAttribute("config",webConfig);
        return "admin/web_config_list/web_config_list_edit";
    }

    @GetMapping("/list")
    @ResponseBody
    @PreAuthorize("hasAuthority('web:config:read')")
    public Map<String,Object> list(String key, int page, int limit){

        Page<WebConfigProperty> webConfigPage = new Page<>(page,limit);

        QueryWrapper<WebConfigProperty> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(key), "id", key);

        List<WebConfigProperty> webConfigs = WebConfigPropertyService.page(webConfigPage,queryWrapper).getRecords();

        Map<String, Object> map = new HashMap<>();

        map.put("code",0);
        map.put("msg","");
        map.put("count",webConfigPage.getTotal());
        map.put("data",webConfigs);
        map.put("key",key);

        return map;
    }

    @PostMapping("/create")
    @ResponseBody
    @PreAuthorize("hasAuthority('web:config:create')")
    public R create(WebConfigProperty webConfig){



        WebConfigPropertyService.save(webConfig);
        return R.success();
    }

    @PostMapping("/del")
    @ResponseBody
    @PreAuthorize("hasAuthority('web:config:delete')")
    public R del(String ids){

        List<Long> list = null;
        if(StrUtil.isNotBlank(ids)){
            list = Stream.of(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        WebConfigPropertyService.removeBatchByIds(list);
        return R.success();
    }
    @PostMapping("/update")
    @ResponseBody
    @PreAuthorize("hasAuthority('web:config:update')")
    public R update(WebConfigProperty webConfig){

        WebConfigPropertyService.updateById(webConfig);
        return R.success();
    }


}
